define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entry/digitalslides/index',
                    add_url: 'entry/digitalslides/add',
                    edit_url: 'entry/digitalslides/edit',
                    del_url: 'entry/digitalslides/del',
                    multi_url: 'entry/digitalslides/multi',
                    table: 'entry_digitalslides',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-add").data("area", ["100%","100%"]);
                $(".btn-editone").data("area", ["100%","100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'classify_id', title: __('Classify_id')},
                        {field: 'title', title: __('Title')},
                        {field: 'hits', title: __('Hits')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(function () {
                classify($(".classify2"), $("input[name='classify1']:checked").val(), 2)
                classify($(".classify3"), $("input[name='classify2']:checked").val(), 3);
            })
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $(".classify1").change(function () {
                    classify($(".classify2"), $("input[name='classify1']:checked").val(), 2);
                })
                $(".classify2").change(function () {
                    classify($(".classify3"), $("input[name='classify2']:checked").val(), 3);
                })

                var tpl = '<div class="row row-files" data-index="<%=index%>">\n' +
                    '                <div class="col-md-12">\n' +
                    '                    <label class="control-label col-xs-12 col-sm-2">切片文件:</label>\n' +
                    '                    <div class="col-xs-12 col-sm-8">\n' +
                    '                        <div class="input-group">\n' +
                    '                            <input type="text" name="row[files][<%=index%>][file]" id="c-file-<%=index%>" class="form-control" />\n' +
                    '                            <div class="input-group-addon no-border no-padding">\n' +
                    '                                <span><button id="plupload-local-<%=index%>" class="btn btn-danger plupload" data-input-id="c-file-<%=index%>" data-url="ajax/upload" data-maxsize="10000MB"><i class="fa fa-upload"></i> 上传</button></span>\n' +
                    '                            </div>\n' +
                    '                        </div>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '                <div class="col-md-12">\n' +
                    '                    <label class="control-label col-xs-12 col-sm-2">切片名称:</label>\n' +
                    '                    <div class="col-xs-12 col-sm-8">\n' +
                    '                        <input type="text" name="row[files][<%=index%>][name]" id="c-local" class="form-control" />\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '                <div class="col-md-12">\n' +
                    '                    <label class="control-label col-xs-12 col-sm-2">切片封面:</label>\n' +
                    '                    <div class="col-xs-12 col-sm-8">\n' +
                    '                        <div class="input-group">\n' +
                    '                            <input id="c-image-<%=index%>" class="form-control" size="50" name="row[files][<%=index%>][image]" type="text" value="">\n' +
                    '                            <div class="input-group-addon no-border no-padding">\n' +
                    '                                <span><button type="button" id="plupload-image-<%=index%>" class="btn btn-danger plupload" data-input-id="c-image-<%=index%>" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-image-<%=index%>"><i class="fa fa-upload"></i> 上传</button></span>\n' +
                    '                                <span><button type="button" id="fachoose-image-<%=index%>" class="btn btn-primary fachoose" data-input-id="c-image-<%=index%>" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>\n' +
                    '                            </div>\n' +
                    '                            <span class="msg-box n-right"></span>\n' +
                    '                        </div>\n' +
                    '                        <ul class="row list-inline plupload-preview" id="p-image-<%=index%>"></ul>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '                <div class="col-md-10 select-remove">\n' +
                    '                    <a href="javascript:;" class="btn btn-sm btn-danger btn-remove-files"><i class="fa fa-times"></i> 移除</a>\n' +
                    '                </div>\n' +
                    '            </div>';
                $('.btn-append-files').click(function(){
                    var index = $(this).prev('.row-files').data('index');
                    index = index ? parseInt(index) : 0;
                    var vars = {index: index+1};
                    var html = Template.render(tpl, vars)
                    $(html).insertBefore($('.btn-append-files'));
                    Upload.api.plupload('.plupload')
                })
                $('.group-files').delegate('.btn-remove-files', 'click', function () {
                    $(this).parents('.row-files').remove()
                })
            }
        }
    };
    return Controller;
});
var step = 5;
function classify(obj, pid, level) {
    $.ajax({
        url: "/admin/ajax/classify",
        type: "GET",
        async:false,
        data: {pid:pid},
        success: function (res) {
            if (res.code == '1') {
                var data = res.data;
                var html = '';
                $(obj).find('.radio').html('');
                var j = 0;
                var checkd = '';
                var name = 'classify'+level;
                if (level == 3) {name = 'row[classify_id]';}
                for (var i in res.data) {
                    checkd = j == 0 ? 'checked' : '';
                    html += ' <label for="classify'+step+'">\n' +
                        '<input id="classify'+step+'" name="'+name+'" '+checkd+' type="radio" value="'+i+'"> '+data[i]+'\n' +
                        '</label>';
                    step++;
                    j++;
                }
                $(obj).find('.radio').append(html);
            }
        },
        error: function (err) {}
    });
}
